package Aleehot100;

import C017.main;

/**
 * ClassName: c040
 * Package: Aleehot100
 * Description: 543. 二叉树的直径
 *
 * @Author BCXJ
 * @Create 2025/7/9 08:38
 * @Version 1.0
 * @Since 1.0
 */
public class c040 {

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        treeNode1.left = treeNode2;
        System.out.println(diameterOfBinaryTree(treeNode1));

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static int ans = 1;

    public static int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        high(root);
        return ans - 1;
    }

    private static int high(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int L = high(node.left);
        int R = high(node.right);
        ans = Math.max(L + R + 1, ans);
        return Math.max(L, R) + 1;
    }

}
